package com.shf.controller;

import com.github.pagehelper.PageInfo;
import com.shf.entity.*;
import com.shf.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 *
 */

@Controller
@RequestMapping("/admin/")
public class OrderController {
    @Autowired
    @Qualifier("os")
    private OrderService orderService;

    @RequestMapping("order/save")
    public String saveOrder(Orders orders) {

        orders.setId(UUID.randomUUID().toString());
        orderService.saveOrder(orders);


        return "forward:/admin/order/order-add.do";
    }

    /**
     * 点击新增按钮 然后查询查询下拉列表的产品 和会员
     */
    @RequestMapping("order/orderAdd")
    public ModelAndView addShow(ModelAndView mv) {
        List<Product> productList = orderService.selectProducts();
        List<Member> memberList = orderService.selectMembers();
        List<Traveller> travellerList = orderService.selectTraveller();
        mv.addObject("productList", productList);
        mv.addObject("memberList", memberList);
        mv.addObject("travellerList",travellerList);
        mv.setViewName("admin/order-add");
        return mv;
    }

    /**
     * 显示所有订单
     *
     * @param page
     * @param size
     * @param mv
     * @return
     */
    @RequestMapping("order/findAll")
    public ModelAndView findAllOrder(@RequestParam("page") Integer page, @RequestParam("size") Integer size, ModelAndView mv) {

        PageInfo<Orders> pageInfo = orderService.findAllOrder(page, size);

        mv.addObject("pageInfo", pageInfo);

        mv.setViewName("admin/order-list");
        return mv;
    }

    /**
     * 显示 订单详情
     */
    @RequestMapping("order/findById")
    public ModelAndView findOrderDetail(@RequestParam(name = "id") String id,ModelAndView mv){

       Orders orders = orderService.findOrderDetail(id);

        List<OrderTraveller> orderTravellers = orders.getOrderTravellers();
        for (OrderTraveller traveller : orderTravellers) {
            System.out.println(traveller);
        }
        mv.addObject("order",orders);

       mv.setViewName("admin/order-show");
       return mv;
    }

    /**
     * 根据id删除订单
     * @param id
     * @return
     */
    @RequestMapping("order/deleteById")
    public String deleteById(@RequestParam("id") String id){
        orderService.deleteById(id);
        return "redirect:/admin/order/findAll.do?page=1&size=5";
    }

    /**
     * 多选删除
     * @param ids
     * @return
     */

    @RequestMapping("order/deleteManny")
    public String deleteManyOrder(@RequestParam("ids") String [] ids){
        orderService.deleteManyOrder(ids);

        return "redirect:/admin/order/findAll.do?page=1&size=5";
    }


    /**
     * 用于修改订单之前的列表回显
     * @return
     */
    @RequestMapping("order/editById")
    public ModelAndView editOrderById(String id,ModelAndView mv){

        Orders orders  = orderService.selectOrderById(id);

        List<Product> productList = orderService.selectProducts();
        List<Member> memberList = orderService.selectMembers();
        mv.addObject("productList", productList);
        mv.addObject("memberList", memberList);
        mv.addObject("order",orders);
        mv.setViewName("admin/order-edit");

        return mv;
    }
    @RequestMapping("order/editOrder")
    public String editOrder(Orders orders){
        orderService.editOrder(orders);

        return "redirect:/admin/order/findAll.do?page=1&size=5";
    }

    @RequestMapping("order/order-add.do")
    public String orderAdd(){
        return "admin/order-add";
    }

}
